package com.itboye.mengwu.app.ui.mymeng.personsettings;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.login.ui.TimeCount;
import com.itboye.mengwu.app.ui.main.MainPresenter;
import com.itboye.mengwu.data.presenter.GetYanzhengMaPresenter;

import java.util.Observable;
import java.util.Observer;

public class ActivityUpdatePwdByPhone extends BaseActivity implements
		OnClickListener, Observer {

	EditText input_bindphone, input_newpwd, input_yzm;
	Button btn_submit;
	Button getYzm;
	private String newPwd;
	MainPresenter mainPresenter;
	GetYanzhengMaPresenter getYanzhengMaPresenter;
	private String uid;
	private String code;
	private String phone;
	TextView txt_title;
	ImageView back;
	private TimeCount count;
	TextView currentPhone;

	String numPHone;
	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_updatepwdbypphone;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		mainPresenter = new MainPresenter(this);
		getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
		uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");
		numPHone=(String) SPUtils.get(this, null, SpPublicParams.PHONE_MODEL, "");
		try {
			if(numPHone==null || numPHone.equals("")){
				currentPhone.setVisibility(View.GONE);
			}else{
				currentPhone.setVisibility(View.VISIBLE);
				currentPhone.setText(numPHone);
				String maskNumber = numPHone.substring(0,3)+"****"+numPHone.substring(7,numPHone.length());
				currentPhone.setText("当前手机:"+maskNumber);
			}
		}catch (Exception e){}


		txt_title.setText("修改登录密码");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.btn_submit) {
			if (phone == null) {
				ByAlert.alert("请输入手机号码");
				return;
			}
			code = input_yzm.getText().toString().trim();
			if (code == null) {
				ByAlert.alert("请输入验证码");
				return;
			}
			newPwd = input_newpwd.getText().toString().trim();
			if (newPwd == null) {
				ByAlert.alert("请输入新密码");
				return;
			}
			showProgressDialog("修改中,请稍后", true);
			btn_submit.setEnabled(false);
			mainPresenter.updatePasswordByTelephone(code, newPwd, phone);
		} else if (v.getId() == R.id.getYzm) {
			phone = input_bindphone.getText().toString().trim();
			if (phone == null || phone.equals("")) {
				ByAlert.alert("请输入手机号码");
				return;
			}
			count = new TimeCount(60000, 1000, getYzm);
			count.start();
			getYanzhengMaPresenter.getVerificationCode(phone, 2 + "");
		}
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaSuccessEvent) {
				ByAlert.alert(entity.getData() + "");
				input_yzm.setText(entity.getData() + "");
			} else if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaFailEvent) {
				ByAlert.alert(entity.getData() + "");
			} else if (entity.getEventType() == MainPresenter.UpdatePwdByPhoneSuccessEvent) {
				ByAlert.alert(entity.getData() + "");
				finish();
			} else if (entity.getEventType() == MainPresenter.UpdatePwdByPhoneFailureEvent) {
				ByAlert.alert(entity.getData() + "");
			}
		}

		btn_submit.setEnabled(true);
	}

}
